package mapper;

import Conn.Conn;
import model.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Singuptext {
    /*
     类: Singuptext
     方法: Addtext
     方法说明:
         获取 id 的 max 值 进行 ++
            并将  姓名 文字 插入到表当中
      */
    private PreparedStatement pstam;
    public boolean Addtext(Test test){
        Conn conn = new Conn();
        try {
            int maxnum = 0;
            pstam = conn.getConn().prepareStatement("select max(Id) from test");
            ResultSet resultSet = pstam.executeQuery();
            if(resultSet.next()){
                maxnum = Integer.parseInt(resultSet.getString("max(Id)"));
            }

//            System.out.println(test.getName());
            pstam = conn.getConn().prepareStatement("insert test(Id,name,text,data) values(?,?,?,?)");
            pstam.setString(1,String.valueOf(maxnum + 1));
            pstam.setString(2,test.getName());
            pstam.setString(3,test.getText());

            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            Calendar calendar = Calendar.getInstance();
            pstam.setString(4,formatter.format(calendar.getTime()));
            int resultSet1 = pstam.executeUpdate();
//            System.out.println(resultSet1);
            if (resultSet1 == 1){
                return true;
            }else {
                return false;
            }
//            pstam
        }catch (Exception e){
            return false;
        }

    }
}
